package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //1.参数效验
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取用户，并判断用户是否登录
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if (apUser.getId() == null) {
            return ResponseResult.okResult(405,"游客身份禁止，需进行登录");
        }
        //2.1获取redis数据，判断是否收藏过
        //2.1.1获取redis中的key值
        String COLLECTION_hash_key = "USER_COLLECTION_" + dto.getArticleId();
        //2.1.2获取判断
        Boolean hasKey = redisTemplate.opsForHash().hasKey(COLLECTION_hash_key, apUser.getId() + "");
        //3.如果用户没有收藏，进行收藏操作
        if (!hasKey && dto.getOperation() == 0) {
            //3.1添加redis中
            redisTemplate.opsForHash().put(COLLECTION_hash_key,apUser.getId() + "","1");
            //3.2添加MongoDB中
            ApCollectionBehavior collectionBehavior = new ApCollectionBehavior();
            BeanUtils.copyProperties(dto,collectionBehavior);
            collectionBehavior.setCollectionTime(new Date());
            collectionBehavior.setId(ObjectId.get().toHexString());
            collectionBehavior.setUserId(apUser.getId().longValue());
            mongoTemplate.insert(collectionBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //4.用户收藏了，继续执行收藏操作则返回
        if (hasKey && dto.getOperation() == 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //4.1用户收藏了，执行取消收藏操作
        if (hasKey && dto.getOperation() == 1) {
            //删除redis数据
            redisTemplate.opsForHash().delete(COLLECTION_hash_key, apUser.getId() + "");
            //删除MongoDB中的数据
            mongoTemplate.remove(
                    Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId()))
                    , ApCollectionBehavior.class);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
